package com.agu.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import javax.annotation.PostConstruct;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 缓存帮助类
 * @author Sansan
 *
 */
@Component
public class CacheUtil {
	/**
	 * 监控缓存名称
	 */
	public static String CACHE_MONITOR = "MONITOR";
	/**
	 * 默认缓存名称
	 */
	public static String CACHE_DEFAULT = "DEFAULT";
	
	private static CacheManager manager;
	
	@Autowired
	private CacheManager manager1;
	
	@PostConstruct
	public void init(){
		manager = manager1;
	}

	public void destroy() {
		if (manager != null) {
			manager.shutdown();
		}
	}

	public static void put(Cache cache,Object key,Object value){
		Element e = new Element(key, value);
		cache.putQuiet(e);
	}
	
	public static Object getValue(Cache cache,Object key) {
		Object value = null;
		if (manager != null) {
			Element e=cache.getQuiet(key);
			if(e!=null){
			value = e.getObjectValue();
			}
		}
		return value;
	}
	
	public static Cache getCache(String name) {
		Cache c = null;
		if (manager != null) {
			c = manager.getCache(name);
		}
		return c;
	}

	public static CacheManager getManager() {
		return manager;
	}

}
